<template>
  <div>
    <div class="box">
      <div class="left" />
      <div class="right">
        <el-form
          ref="formdataRef"
          class="deng"
          label-width="100px"
          :model="formdata"
          :rules="formdatarow"
        >
          <el-form-item>
            <h1>登录</h1>
          </el-form-item>
          <el-form-item prop="mobile">
            <el-input v-model="formdata.mobile" placeholder="请输入账号" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="formdata.password" type="password" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item prop="isAgree">
            <el-checkbox
              v-model="formdata.isAgree"
            >用户平台使用协议</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="doClick">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    var validateIsAgree = (rule, value, callback) => {
      value ? callback() : callback(new Error('淦,选上'))
    }
    return {
      formdata: {
        mobile: '13800000002',
        password: '123456',
        isAgree: false
      },
      formdatarow: {
        mobile: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { pattern: /^1[3|5|6|7|8|9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { pattern: /^\w{6,15}$/, message: '密码为6-15位', trigger: 'blur' }
        ],
        isAgree: [{ 'validator': validateIsAgree, 'trigger': 'change' }]
      }
    }
  },
  methods: {
    doClick() {
      // 发送axiso
      this.$refs.formdataRef.validate(async valid => {
        if (valid) {
          try {
          // promise 的出现是为了解决 回调地狱的问题
          // es7 里面 又出现了一种语法， async + await 的方式，简化异步代码的书写
          // 将异步代码当做同步的方式去书写，本质上还是异步
            // const res = await getInfo({ mobile: this.ruleForm.mobile, password: this.ruleForm.password })
            // console.log(res)
            // 调用vuex里面的actions 把token储存起来
            this.$store.dispatch('user/loginAction', {
              mobile: this.formdata.mobile,
              password: this.formdata.password
            })
          } catch (err) {
            console.log(1, err)
          }
        } else {
          console.log('有某一个校验失败')
          return false
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: row;
  h1 {
    margin-left: 140px;
  }
  .left {
    flex: 3;
    background-image: url("../../assets/common/login_back.png");
    background-size: 100%;
  }
  .right {
    flex: 2.32;
    display: flex;
    justify-content: center;
    align-items: center;
    .deng {
      width: 450px;
      .el-button {
        width: 350px;
      }
    }
  }
}
</style>
